using System;
namespace ch.ethz.ssh2.util
{

    /// <summary> Tokenizer. Why? Because StringTokenizer is not available in J2ME.
    /// 
    /// </summary>
    /// <author>  Christian Plattner
    /// </author>
    /// <version>  2.50, 03/15/10
    /// </version>
    public class Tokenizer
    {
        /// <summary> Exists because StringTokenizer is not available in J2ME.
        /// Returns an array with at least 1 entry.
        /// 
        /// </summary>
        /// <param name="source">must be non-null
        /// </param>
        /// <param name="delimiter">
        /// </param>
        /// <returns> an array of Strings
        /// </returns>
        public static System.String[] parseTokens(System.String source, char delimiter)
        {
            int numtoken = 1;

            for (int i = 0; i < source.Length; i++)
            {
                if (source[i] == delimiter)
                    numtoken++;
            }

            System.String[] list = new System.String[numtoken];
            int nextfield = 0;

            for (int i = 0; i < numtoken; i++)
            {
                if (nextfield >= source.Length)
                {
                    list[i] = "";
                }
                else
                {
                    int idx = source.IndexOf(delimiter, nextfield);
                    if (idx == -1)
                        idx = source.Length;
                    list[i] = source.Substring(nextfield, (idx) - (nextfield));
                    nextfield = idx + 1;
                }
            }

            return list;
        }
    }
}